package com.pc.scrs.controller;

import com.pc.scrs.bean.vo.EmailVo;
import com.pc.scrs.bean.vo.MobileVo;
import com.pc.scrs.bean.vo.TeacherVo;
import com.pc.scrs.bean.vo.pageInfo.TeacherListVoPageInfo;
import com.pc.scrs.service.ITeacherService;
import com.pc.scrs.utils.ResponseResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Resource
    private ITeacherService teacherService;

    @GetMapping("/teacherList")
    public ResponseResult queryTeacherList(
            @RequestParam(value = "mobile", required = false, defaultValue = "") String mobile,
            @RequestParam(value = "email", required = false, defaultValue = "") String email,
            @RequestParam(value = "enable", required = false, defaultValue = "") Integer enable,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        TeacherListVoPageInfo pageInfo = teacherService.teacherList(mobile, email, enable, pageNum, pageSize);
        if (pageInfo != null) {
            return new ResponseResult(200, "ok", pageInfo);
        } else {
            return ResponseResult.FAIL;
        }
    }

    @GetMapping("/mobileList")
    public ResponseResult queryMobile() {
        List<MobileVo> list = teacherService.mobileList();
        if (list != null) {
            return new ResponseResult(200, "ok", list);
        } else {
            return ResponseResult.FAIL;
        }
    }

    @GetMapping("/emailList")
    public ResponseResult queryEmail() {
        List<EmailVo> list = teacherService.emailList();
        if (list != null) {
            return new ResponseResult(200, "ok", list);
        } else {
            return ResponseResult.FAIL;
        }
    }

    /**
     * 验证禁用教师权限
     * @return
     */
    @GetMapping ("/showDisableAuthority")
    @PreAuthorize("hasAuthority('teacher:disable')")
    public ResponseResult disableAuthority(){
        return new ResponseResult<>(200, "ok");
    }

    @GetMapping("/disable")
    public ResponseResult disableStudent(Integer id){
        Integer result = teacherService.disableTeacher(id);
        return new ResponseResult(200, "ok",result);
    }

    /**
     * 验证启用教师权限
     * @return
     */
    @GetMapping ("/showEnableAuthority")
    @PreAuthorize("hasAuthority('teacher:enable')")
    public ResponseResult enableAuthority(){
        return new ResponseResult<>(200, "ok");
    }

    @GetMapping("/enable")
    public ResponseResult enableStudent(Integer id){
        Integer result = teacherService.enableTeacher(id);
        return new ResponseResult(200, "ok",result);
    }

    /**
     * 验证修改密码权限
     * @return
     */
    @GetMapping ("/showUpdatePasswordAuthority")
    @PreAuthorize("hasAuthority('teacher:updatePassword')")
    public ResponseResult updatePasswordAuthority(){
        return new ResponseResult<>(200, "ok");
    }

    @GetMapping("/updatePassword")
    public ResponseResult updatePassword(Integer id, @RequestParam("newPassword") String password){
        Integer result = teacherService.updatePassword(id, password);
        return new ResponseResult(200, "ok",result);
    }

    @GetMapping("/queryTeacherById")
    public ResponseResult queryTeacherById(Integer id){
        TeacherVo teacherVo = teacherService.queryTeacherById(id);
        return new ResponseResult(200, "ok",teacherVo);
    }

    /**
     * 验证修改教师信息权限
     * @return
     */
    @GetMapping ("/showUpdateProfileAuthority")
    @PreAuthorize("hasAuthority('teacher:updateProfile')")
    public ResponseResult updateProfileAuthority(){
        return new ResponseResult<>(200, "ok");
    }

    @PostMapping("/updateProfile")
    public ResponseResult updateTeacher(@RequestBody TeacherVo teacherVo){
        Integer result = teacherService.updateTeacher(teacherVo);
        if(result>0){
            return new ResponseResult(200, "ok");
        }else {
            return ResponseResult.FAIL;
        }
    }

    /**
     * 验证添加教师权限
     * @return
     */
    @GetMapping ("/showAddAuthority")
    @PreAuthorize("hasAuthority('teacher:add')")
    public ResponseResult addAuthority(){
        return new ResponseResult<>(200, "ok");
    }

    @PostMapping("/add")
    public ResponseResult addTeacher(@RequestBody TeacherVo teacherVo){
        Integer result = teacherService.addTeacher(teacherVo);
        if(result>0){
            return new ResponseResult(200, "ok");
        }else {
            return ResponseResult.FAIL;
        }
    }
}
